package com.hcbd.tdwt.ui.setting.hlct;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.core.content.ContextCompat;


import com.hcbd.tdwt.R;
import com.hcbd.tdwt.base.BaseActivity;
import com.hcbd.tdwt.base.BaseSpinner;
import com.hcbd.tdwt.base.BeidouBoxParams;
import com.pangu.bdsdk2021.entity.extensionterminal.DwZxxInfo;
import com.pangu.bdsdk2021.terminal.TerminalSendManager;
import com.hcbd.tdwt.ui.xingyu.activity.AddressBookActivity_;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.OnActivityResult;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

@SuppressLint("NonConstantResourceId")
@EActivity(R.layout.activity_set_location_reporting_tt)
public class SetLocationReportingActivity extends BaseActivity {
    @ViewById
    TextView title;
    @ViewById
    ImageButton headLeft;
    @ViewById
    RelativeLayout topRL;
    @ViewById
    EditText bdCardEt,receiveNameTv,frequencyEt;
    @ViewById
    Button saveBtn;
    @ViewById
    Spinner spinner;
    private BaseSpinner baseSpinner;
    private final int SELECT_CONTACT_CODE=15;
    @AfterViews
    protected void init() {
        title.setText("位置上报设置信息");
        title.setTextColor(ContextCompat.getColor(this,R.color.black));
        headLeft.setBackground(ContextCompat.getDrawable(this,R.drawable.left_arrow));
        topRL.setBackground(ContextCompat.getDrawable(this,R.color.white));
        setAndroidNativeLightStatusBar(true);
        baseSpinner = new BaseSpinner(this);
        baseSpinner.initSprinnerByKeyWhile(spinner, R.array.select_fre_value, R.array.select_fre_key, "60秒");
        initMsgRegister();
        if(BeidouBoxParams.isBoxConnectNormal){
            try {
                showLoading("加载中");
                TerminalSendManager.getInstance().sendFCxWz(0, 0, 0);
            }catch (Exception e){
                e.printStackTrace();
            }
        }else{
            toast("未连接蓝牙,无法使用该功能!");
        }
    }
    @Click
    void openContactBtn(){
        Intent intent=new Intent();
        intent.setClass(this, AddressBookActivity_.class);
        intent.putExtra("entryMode",1);
        startActivityForResult(intent,SELECT_CONTACT_CODE);
    }
    @OnActivityResult(SELECT_CONTACT_CODE)
    void selectContactResult(int resultCode, Intent data){
        if(resultCode==RESULT_OK){
            String receiverNumber=data.getStringExtra("receiverNumber");
            String receiverName=data.getStringExtra("receiverName");
            bdCardEt.setText(receiverNumber);
            receiveNameTv.setText(receiverName);
        }
    }
    @Click
    void saveBtn(){
        if(!BeidouBoxParams.isBoxConnectNormal){
            toast("未连接蓝牙!");
            return;
        }
        String number=getTextStr(bdCardEt);
        if("".equals(number)){
            toast("请输入或者选择卡号!");
            return;
        }
        String frequency=getTextStr(frequencyEt);
        if("".equals(frequency)){
            toast("请输入上报频度!");
            return;
        }
        int fre=Integer.parseInt(frequency);
        if(0>=fre){
            toast("请输入正确的频度!");
            return;
        }
        TerminalSendManager.getInstance().sendFCxWz(1,Integer.parseInt(number),fre);
        toast("已设置！");

    }
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true, priority = 1)
    public void eventBus(DwZxxInfo dwZxxInfo) {
        if(dwZxxInfo!=null){
            bdCardEt.setText(dwZxxInfo.centerNumber);
           // baseSpinner.initSprinnerByKeyWhile(spinner,R.array.select_fre_value, R.array.select_fre_key, dwZxxInfo.reportingCycle);
            frequencyEt.setText(dwZxxInfo.reportingCycle);
        }
        hideLoading();
    }
    @Click
    void headLeft(){
        finish();
    }
}
